#include "main.h"

void cl_exit (int function,int errorCode)
{
	int i = 0;
#ifdef __DEBUG_
	printf ("\n function: ");
	switch (function)	{
	case 1:
		printf ("Add files ");
		break;
	case 2:
		printf ("Func ini ");
		break;
	case 3:
		printf ("Listing file ");
		break;
	case 4:
		printf ("open arc ");
		break;
	case 5:
		printf ("testing ");
		break;
	case 6:
		printf ("copy arc ");
		break;
	case 7:
		printf ("copy ");
		break;
	case 8:
		printf ("clean files ");
		break;
	case 9:
		printf ("make_file_head ");
		break;
	case 10:
		printf ("intwrite ");
		break;
	case 11:
		printf ("write to arc ");
		break;
	case 12:
		printf ("extr file ker ");
		break;
	case 13:
		printf ("del_file ");
		break;
	case 14:
		printf ("huff tree init ");
		break;
	case 15:
		printf ("make huff tree ");
		break;
	case 16:
		printf ("paste ");
		break;
	case 17:
		printf ("extrfile_ui");
		break;
	case 19:
		printf ("str write ");
		break;
	case 20:
		printf (" str to huftree ");
		break;
	case 21:
		printf ("print ");
		break;
	case 22:
		printf ("sort_hufftree ");
		break;
	case 23:
		printf ("compress ");
		break;
	case 24:
		printf ("makeTables");
		break;
	case 25:
		printf (" huftree to string ");
		break;
	case 26:
		printf ("make arr huftree");
		break;
	case 27:
		printf (" decompress ");
		break;
	case 28:
		printf ("bits in char ");
		break;
	}
	#endif
	printf ("\n");
	switch (errorCode)	{
	case 1:
		printf ("sign err ");
		break;
	case 2:
		printf ("memory err ");
		break;
	case 3:
		printf ("arc iwrite ");
		break;
	case 4:
		printf ("open file err ");
		break;
	case 5:
		printf ("open_err ");
		break;
	case 6:
		printf ("read err ");
		break;
	case 7:
		printf (" write_err ");
		break;
	case 8:
		printf ("write_err 2 ");
		break;
	case 9:
		printf ("check sum err");
		break;
	case 10:
		printf (" arc to arc");
		break;
	case 11:
		printf ("err arc del ");
		break;
	case 12:
		printf (" err tmp file ren ");
		break;
	case 13:
		printf (" err arc ren ");
		break;
	case 14:
		printf (" err file rename ");
		break;
	case 15:
		printf (" err null str ");
		break;
	case 16:
		printf ("err repl old file");
		break;
	case 17:
		printf (" err arc open ");
		break;
	case 18:
		printf (" copy err ");
		break;
	case 19:
		printf (" error name arc ");
		break;
	case 20:
		printf ("incorret data ");
		break;
	}
	printf ("\n");

	if (envInf.old_arc)
		fclose(envInf.old_arc);
	if	(envInf.new_arc)
		fclose (envInf.new_arc);
	if (envInf.fileToRead)
		fclose(envInf.fileToRead);
	if (envInf.fileToWrite)
		fclose(envInf.fileToWrite);
	if (envInf.tmpFile_1)
		fclose (envInf.tmpFile_1);
	if (envInf.tmpArcName)	{
		remove (envInf.tmpArcName);
	}
	if (envInf.tmpFileName)	{
		remove (envInf.tmpFileName);
	}
	if  (envInf.buf)
		free (envInf.buf);
	if	(envInf.hufTreeinStr)
		free (envInf.hufTreeinStr);
	if (envInf.buf_2)
		free (envInf.buf_2);
	if (envInf.tables)	{
		i = 0;
		while (envInf.tables[i])	{
			free (envInf.tables[i]);
			i++;
		}
		free (envInf.tables);
	}
	if (envInf.HufTree)
		clean_tree (envInf.HufTree);
	else 
		if (envInf.HufTree_list)	{
			i =  0;
			while ((envInf.HufTree_list[i]) && (i<256))	{
				free (envInf.HufTree_list[i]);
				i++;	}
			free (envInf.HufTree_list);
		}
	if (envInf.list)	{
		i = 0;
		while (envInf.list[i])
		{
			free(envInf.list[i]);
			i++;
		}
		free (envInf.list);	}
	if (envInf.fileStat)
		free (envInf.fileStat);
	if (envInf.inf)
		free (envInf.inf);
	if (envInf.inf_2)
		free (envInf.inf_2);
	longjmp (errorBuf,errorCode);
}